import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:window_manager/window_manager.dart';
import 'dart:io';

import 'app/routes/app_pages.dart';
import 'app/core/bindings/initial_binding.dart';
import 'app/data/services/storage_service.dart';
import 'app/core/theme/app_theme.dart';

/// 应用程序入口 - Application Entry Point
Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized(); // 必须要添加这个进行初始化 否则下面会错误

  // Windows 桌面窗口管理初始化 - Windows desktop window management initialization
  if (Platform.isWindows) {
    await windowManager.ensureInitialized();

    WindowOptions windowOptions = const WindowOptions(
      size: Size(1200, 800),
      minimumSize: Size(800, 600),
      center: true,
      backgroundColor: Colors.transparent,
      skipTaskbar: false,
      titleBarStyle: TitleBarStyle.normal,
      windowButtonVisibility: true,
    );

    windowManager.waitUntilReadyToShow(windowOptions, () async {
      await windowManager.show();
      await windowManager.focus();
    });
  }

  // 初始化存储服务 - 必须使用异步方式 - Initialize storage service asynchronously
  await Get.putAsync(
      () => StorageService().onInit().then((_) => StorageService()));
  // Get.put(StorageService(), permanent: true);
  // 暂时注释API服务初始化 - 未连接后端 - Temporarily comment out API service initialization - Backend not connected
  // 初始化 API 服务 - Initialize API service
  // Get.put(ApiService(), permanent: true);

  // 初始化 API 仓库 - 依赖于 ApiService - Initialize API repository depending on ApiService
  // Get.put(ApiRepository(), permanent: true);

  // 设置屏幕方向为竖屏 - Set screen orientation to portrait
  SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp])
      .then((_) {
    runApp(const PromptManagerApp());
  });
}

/// Prompt 管理应用 - Prompt Manager Application
class PromptManagerApp extends StatelessWidget {
  const PromptManagerApp({super.key});

  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      title: 'AI提示词管理',
      debugShowCheckedModeBanner: false,

      // 主题配置 - Theme Configuration
      theme: AppTheme.lightTheme(),

      // 暗色主题配置 - Dark Theme Configuration
      darkTheme: AppTheme.darkTheme(),

      // 主题模式 - Theme Mode
      themeMode: ThemeMode.system,

      // 路由配置 - Route Configuration
      // 根据平台决定初始路由 - Determine initial route based on platform
      initialRoute: Platform.isAndroid
          ? Routes.permission
          : Platform.isWindows
              ? Routes.splash
              : Routes.home,
      getPages: AppPages.routes,

      // 初始绑定 - Initial Binding
      initialBinding: InitialBinding(),

      // 国际化配置 - Internationalization Configuration
      // locale: const Locale('zh', 'CN'),
      // fallbackLocale: const Locale('en', 'US'),

      // 默认过渡动画 - Default Transition Animation
      // defaultTransition: Transition.cupertino,
      // transitionDuration: const Duration(milliseconds: 300),

      // 文字大小不随系统设置改变 - Text scale factor fixed
      builder: (context, widget) {
        return MediaQuery(
          ///设置文字大小不随系统设置改变 - Set text size not to change with system settings
          data: MediaQuery.of(context)
              .copyWith(textScaler: const TextScaler.linear(1.0)),
          child: widget!,
        );
      },
    );
  }
}
